using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Walkmen.Util;

namespace Walkmen.ID3v2
{
    public class ID3v2APICFrame: ID3v2Frame
    {
        private ID3v2Encoding _encoding;
        private string _mimeType;
        private byte _pictureType;
        private string _description;
        private byte[] _content;

        public ID3v2Encoding Encoding
        {
            get { return _encoding; }
            set { _encoding = value; }
        }

        public string MimeType
        {
            get { return _mimeType; }
            set { _mimeType = value; }
        }

        public byte PictureType
        {
            get { return _pictureType; }
            set { _pictureType = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public byte[] Content
        {
            get { return _content; }
            set { _content = value; }
        }

        protected override byte[] CreateValue()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                stream.WriteByte((byte)_encoding);

                byte[] mimeType = System.Text.Encoding.ASCII.GetBytes(_mimeType + "\0");
                stream.Write(mimeType, 0, mimeType.Length);

                stream.WriteByte(_pictureType);

                Encoding encoding = CodeToEncoding(_encoding);

                byte[] description = encoding.GetBytes(_description + "\0");
                stream.Write(description, 0, description.Length);

                stream.Write(_content, 0, _content.Length);

                return stream.ToArray();
            }
        }
    }
}
